package com.galaxy.samson.stock.dao;

import com.galaxy.samson.stock.domain.Stock;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: arunachalam
 * Date: 2/6/12
 * Time: 7:42 PM
 * To change this template use File | Settings | File Templates.
 */


public class StockDao implements IStockDao
{
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void insert(Stock stock)
    {
             entityManager.persist(stock);
    }

    @Override
    public void removeAll()
    {
        Query query = entityManager.createNamedQuery("removeAllStock");
        query.executeUpdate();
    }

    @Override
    public void remove(String productName)
    {
        Query query = entityManager.createQuery("delete s from Stock s where s.productName=:item");
        query.setParameter("item",productName);
        query.executeUpdate();
//        entityManager.remove(student);
    }

    @Override
    public List<Stock> findAll()
    {
        Query query = entityManager.createQuery("select s from Stock s");
        return query.getResultList();
    }

    @Override
    public List<Stock> searchByCategory(String category)
    {
        Query query = entityManager.createNamedQuery("searchByCategory");
        query.setParameter("category",category);
        return query.getResultList();

    }

    @Override
    public List<Stock> searchByName(String name)
    {
        Query query = entityManager.createNamedQuery("searchByProductName");
        query.setParameter("productName",name);
        return query.getResultList();
    }
}
